/*
    Dijjer - A Peer to Peer HTTP Cache
    Copyright (C) 2008 Mathias De Maré

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package dijjer;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.StringTokenizer;

import dijjer.io.comm.AbstractMulticastSocketManager;
import dijjer.io.comm.AbstractPeer;
import dijjer.io.comm.AbstractRoutingTable;
import dijjer.io.comm.AbstractUdpSocketManager;
import dijjer.io.comm.WebMulticastSocketManager;
import dijjer.io.comm.WebPeer;
import dijjer.io.comm.WebRoutingTable;
import dijjer.io.comm.WebUdpSocketManager;
import dijjer.updater.AbstractResourceManager;
import dijjer.updater.WebResourceManager;
import dijjer.util.logging.Logger;

public class WebFactory extends AbstractFactory {
	public WebFactory() {}

	public AbstractPeer getPeer(String destString) throws Exception {
		return new WebPeer(destString);
	}

	public AbstractUdpSocketManager getUdpSocketManager() {
		return WebUdpSocketManager.getUdpSocketManager();
	}

	public AbstractMulticastSocketManager getMulticastSocketManager() {
		return WebMulticastSocketManager.getMulticastSocketManager();
	}

	public AbstractResourceManager getResourceManager() {
		if(!resourceManagerSet) {
			rm = new WebResourceManager();
			resourceManagerSet = true;
		}
		return rm;
	}

	public boolean isSiteLocal(String addr) {
		try {
			InetAddress local = InetAddress.getByName(addr);
			if(local.isSiteLocalAddress())
				return true;
		}
		catch(UnknownHostException e) {
			Logger.warning("Failed to check address. Are we connected to the internet?", e);
			return false;
		}
		return false;
	}

	public void initUdpSocketManager(int externalListenPort) throws Exception {
		WebUdpSocketManager.init(externalListenPort);
	}

	public void initMulticastSocketManager(int externalListenPort) throws Exception {
		WebMulticastSocketManager.init(externalListenPort);
	}

	public AbstractPeer getPeer() throws Exception {
		return new WebPeer();
	}

	public AbstractPeer getPeer(DataInputStream dis) throws Exception {
		return new WebPeer(dis);
	}
	
	public AbstractPeer getMulticastPeer() throws Exception {
		AbstractResourceManager arm = AbstractFactory.getFactory().getResourceManager();
		return new WebPeer(arm.getMulticastAddress() + ":" + arm.getMulticastPort());
	}

	public void initRoutingTable(int routingTableSize) throws Exception {
		WebRoutingTable.init(routingTableSize);
	}

	public AbstractRoutingTable getRoutingTable() {
		return WebRoutingTable.getRoutingTable();
	}

}
